<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="4"
      labelWidth="120"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="jzyBrand"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <!-- <el-button size="small" type="primary" @click="addHandle">新增</el-button> -->
        <multiuse-button v-show="dealerType !== '25381002'" size="small" type="primary" :click="addHandle" :auth-list="auths" auth-mark="/api/basedata/vehicleProduct/brands">新增</multiuse-button>
				<multiuse-button size="small" type="primary" icon="el-icon-download" :loading="exportLoading"  :click="exportHandle" :auth-list="auths" auth-mark="/api/2">下载</multiuse-button>
        <!-- <el-button size="small">导入</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      width="30%"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <BrandModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getBrandsOEM,getBrandListOEM,exportBrands} from '@/api/common/cars/brandcarsModels';
import BrandModal from './brandModal';
import { authority } from '@/utils/authMixin';
import Cookies from 'js-cookie';
import { exportExcelFile } from '@/utils';

export default {
  name: 'Brand',
  mixins: [dictionary,authority],
  components: {
    BrandModal
  },
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: getBrandsOEM,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      dealerType: Cookies.get('dealerType'),
      exportLoading: false
    };
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '品牌名称',
          fieldName: 'brandName',
          placeholder: '请选择...',
          itemList:[]
        },
        {
          type: 'INPUT',
          label: '品牌代码',
          fieldName: 'brandCode',
          placeholder: '请输入...'
        },
        {
          type: 'SELECT',
          label: '是否整车销售',
          fieldName: 'isVehicleSalesBrand',
          placeholder: '请选择...',
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择...',
          itemList: this.createDictList('1004')
        }
      ];
    },
    createTableColumns() {
      let dealerType = Cookies.get('dealerType');
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <multiuse-button containerStyle={dealerType !== '25381002' ? {}:{display:'none'}} size="mini" type="text" onClick={() => this.editHandle(props.row)} auth-list={this.auths} auth-mark={'/api/basedata/vehicleProduct/brands/${id}'}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '品牌代码',
          dataIndex: 'brandCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '品牌名称',
          dataIndex: 'brandName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否整车销售',
          dataIndex: 'isVehicleSalesBrand',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isVehicleSalesBrand, '1004')}</span>;
          }
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1004')}</span>;
          }
        }
      ];
    },
    init(){
      getBrandListOEM().then(res=>{
        console.log(res,"品牌")
        res.data.map(x=>{
          this.topFilterList.find(item=>item.fieldName == "brandName").itemList.push({text:x.brandName,value:x.brandCode})
        })
      })
    },

// 导出
    async exportHandle(row) {
      let params = JSON.parse(JSON.stringify(this.fetchParams));
      this.handleType = 'export';
      this.exportLoading = true;
      await exportBrands({ ...params, userId: this.userId })
        .then(response => {
          // console.log(response,"response")
          exportExcelFile(response.data, '品牌信息');
          this.exportLoading = false;
        })
        .catch(err => {
          this.exportLoading = false;
          console.log(err);
        });
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}品牌`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.init()
  }
};
</script>

<style lang="less" scoped>
</style>
